package com.zlc.gulimall.product.controller;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.zlc.gulimall.product.entity.BrandEntity;
import com.zlc.gulimall.product.service.BrandService;
import com.zlc.gulimall.product.vo.BrandRespVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.zlc.gulimall.product.entity.CategoryBrandRelationEntity;
import com.zlc.gulimall.product.service.CategoryBrandRelationService;
import com.zlc.common.utils.PageUtils;
import com.zlc.common.utils.R;

import javax.annotation.Resource;
import javax.websocket.server.PathParam;


/**
 * 品牌分类关联
 *
 * @author zlc
 * @email sunlightcs@gmail.com
 * @date 2024-09-16 13:43:31
 */
@Slf4j
@RestController
@RequestMapping("product/categorybrandrelation")
public class CategoryBrandRelationController {
    @Autowired
    private CategoryBrandRelationService categoryBrandRelationService;

    @Resource
    BrandService brandService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = categoryBrandRelationService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    public R info(@PathVariable("id") Long id){
		CategoryBrandRelationEntity categoryBrandRelation = categoryBrandRelationService.getById(id);

        return R.ok().put("categoryBrandRelation", categoryBrandRelation);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    public R save(@RequestBody CategoryBrandRelationEntity categoryBrandRelation){
        categoryBrandRelationService.saveDetails(categoryBrandRelation);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public R update(@RequestBody CategoryBrandRelationEntity categoryBrandRelation){
		categoryBrandRelationService.updateById(categoryBrandRelation);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public R delete(@RequestBody Long[] ids){
		categoryBrandRelationService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

    /*
    * 根据catId查询品牌
    * */
    @GetMapping("/brands/list")
    public R brandList(@PathParam("catId") Long catId){
        List<BrandEntity> brands = categoryBrandRelationService.queryByCatId(catId);
        List<BrandRespVo> list = categoryBrandRelationService.transToBrandRespVo(brands);

        return R.ok().put("data",list);
    }

}
